package com.lt.contract.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lt.contract.bean.utils.ResponseResult;
import com.lt.contract.bean.utils.Utils;
import com.lt.contract.entity.NmErpContractRelation;
import com.lt.contract.service.NmErpContractRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * ERP下发合同明细计费方式维护(NmErpContractRelation)表控制层
 *
 * @author makejava
 * @since 2020-02-18 21:29:42
 */
@RequestMapping("/nmErpContractRelation")
@RestController
@Slf4j
public class NmErpContractRelationController {
    /**
     * 服务对象
     */
    @Resource
    private NmErpContractRelationService nmErpContractRelationService;

    /**
     * 获取  ERP下发合同明细计费方式维护(NmErpContractRelation)
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getContractImportantList")
    public JSONArray getContractImportantList(@RequestParam String params){
        List<NmErpContractRelation> contractList = null;
        try {
            Map<String,Object> map = JSON.parseObject(params);
            contractList = nmErpContractRelationService.getContractImportantList(map);
            JSONArray result = JSON.parseArray(JSON.toJSONString(contractList));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 获取  ERP下发合同明细计费方式维护(NmErpContractRelation)
     * @param params
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/getContractImportantCount")
    public Long getContractImportantCount(@RequestParam String params){
        try {
            Map<String,Object> map = JSON.parseObject(params);
            Long count = nmErpContractRelationService.getContractImportantCount(map);
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * 新增ERP下发合同明细计费方式维护(NmErpContractRelation)
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid NmErpContractRelation renew, BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                String s = Utils.hasErrors(bindingResult);
                return new ResponseResult(false, "插入失败", s);
            }
            int code = nmErpContractRelationService.insert(renew);
            if(code<=0){
                return  new ResponseResult(false, "插入失败");
            }
            return new ResponseResult(true, "插入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false, "插入失败", e.getStackTrace());
        }
    }

    /**
     * 更新 ERP下发合同明细计费方式维护(NmErpContractRelation)
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid NmErpContractRelation renew, BindingResult bindingResult){
        try {
            if (bindingResult.hasErrors()) {
                String s = Utils.hasErrors(bindingResult);
                return new ResponseResult(false, "update失败", s);
            }
            if (renew.getContractNoGlobal() ==null || renew.getContractNoGlobal().equals("")){
                return new ResponseResult(false, "update失败", "更新请传入合同编号");
            }
            int code = nmErpContractRelationService.update(renew);
            if(code<=0){
                return  new ResponseResult(false, "更新失败");
            }
            return new ResponseResult(true,"更新成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false,"更新失败",e.getStackTrace());
        }
    }

    /**
     * 删除 ERP下发合同明细计费方式维护(NmErpContractRelation)  update data_tag 0 有效数据 1 删除数据
     * @param params
     * @return
     */
    @CrossOrigin
    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody String params){
        try {
            JSONObject renew = JSON.parseObject(params);
            if(renew.getString("contractNoGlobal") == null ||renew.getString("contractNoGlobal").equals("")){
                return new ResponseResult(false, "delete失败", "请传入主键");
            }else if(renew.getString("staffId") == null ||renew.getString("staffId").equals("")){
                return new ResponseResult(false, "delete失败", "");
            }
            NmErpContractRelation record = JSON.parseObject(params,NmErpContractRelation.class);
            nmErpContractRelationService.deleteById(record);
            return new ResponseResult(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(false,"删除失败",e.getStackTrace());
        }
    }





}
